package com.neiru.divider.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.Toast;

import com.neiru.divider.R;
import com.neiru.divider.bean.Level;
import com.neiru.divider.bean.Player;
import com.neiru.divider.constants.ActivityConstants;
import com.neiru.divider.facade.LevelFacade;
import com.neiru.divider.facade.PlayerFacade;

public class Main extends Activity {
	private Player player;
	private PlayerFacade playerFacade;
	private LevelFacade levelFacade;
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.main);
		initializeValues();
		configureButtons();
	}
	
	private void initializeValues() {
		this.playerFacade = new PlayerFacade(this);
		this.levelFacade = new LevelFacade(this);
		this.player = this.playerFacade.getPlayer();
		this.levelFacade.initializeLevels();
	}
	
	protected void onResume() {
		super.onResume();
		this.player = this.playerFacade.getPlayer();
	}
	
	private void configureButtons() {
		ImageButton playButton = (ImageButton)findViewById(R.id.play_button);
		playButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Level level = Main.this.levelFacade.getLevel(player.getLastLevel());
				Intent i = new Intent(Main.this, Play.class);
				i.putExtra(ActivityConstants.PLAY_LEVEL_DATA_NAME, level);
				startActivityForResult(i, ActivityConstants.ACTIVITY_PLAY);
			}
		});
		ImageButton levelListButton = (ImageButton)findViewById(R.id.level_list);
		levelListButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Level level = Main.this.levelFacade.getLevel(player.getLastLevel());
				Intent i = new Intent(Main.this, Levels.class);
				i.putExtra(ActivityConstants.PLAY_LEVEL_DATA_NAME, level);
				i.putExtra(ActivityConstants.PLAY_LEVEL_DATA_PLAYER, player);
				startActivityForResult(i, ActivityConstants.ACTIVITY_LEVELS);
			}
		});
		
		ImageButton scoreButton = (ImageButton)findViewById(R.id.scores);
		scoreButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(Main.this, Score.class);
				i.putExtra(ActivityConstants.PLAY_LEVEL_DATA_PLAYER, player);
				startActivity(i);
			}
		});
		
		ImageButton aboutButton = (ImageButton) findViewById(R.id.about);
		aboutButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(Main.this, About.class);
				startActivity(i);
			}
		});
		
		ImageButton exitButton = (ImageButton) findViewById(R.id.exit);
		exitButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Main.this.finish();
			}
		});
	}
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		Log.d("Main",requestCode+"=="+ActivityConstants.ACTIVITY_PLAY+"??");
		switch(requestCode) {
			case ActivityConstants.ACTIVITY_PLAY:
				if(resultCode == ActivityConstants.PLAY_RESULT_NEXT_LEVEL) {
					Level passedLevel = (Level)data.getExtras().getSerializable(ActivityConstants.PLAY_LEVEL_DATA_NAME);
					Long score = data.getExtras().getLong(ActivityConstants.PLAY_LEVEL_DATA_SCORE);
					if(score != null) {
						player.setScore( ((Long)(player.getScore()+score)).intValue());
					}
					
					if(passedLevel.getLevel() >= this.levelFacade.getLastLevel()) {
						//Si se ha pasado el juego, sacar la pantalla de congratulations you are very coool
						return;
					}else if(passedLevel.getLevel() >= 1) { //player.getLastLevel()) 	{
						player.setLastLevel(passedLevel.getLevel() + 1);
					}
					playerFacade.updatePlayer(player);
					
					//Iniciar la siguiente pantalla
					Level level = Main.this.levelFacade.getLevel(player.getLastLevel());
					Intent i = new Intent(Main.this, Play.class);
					i.putExtra(ActivityConstants.PLAY_LEVEL_DATA_NAME, level);
					startActivityForResult(i, ActivityConstants.ACTIVITY_PLAY);
				} else if(resultCode == ActivityConstants.PLAY_RESULT_FINISH) {
					Level passedLevel = (Level)data.getExtras().getSerializable(ActivityConstants.PLAY_LEVEL_DATA_NAME);
					//TODO Cambiar esto, para que guarde el Score
					if(passedLevel.getLevel() >= player.getLastLevel()) 	{
						player.setLastLevel(passedLevel.getLevel() + 1);
					}
					Long score = data.getExtras().getLong(ActivityConstants.PLAY_LEVEL_DATA_SCORE);
					if(score != null){
						player.setScore( ((Long)(player.getScore()+score)).intValue());
					}
					playerFacade.updatePlayer(player);
					
				} else {
					showGameOverMessage();
				}
				break;
		}
	}
	private void showGameOverMessage() {
		Toast.makeText(this, "GAME OVER", Toast.LENGTH_LONG).show();
	}
}